package com.ylh.user.dao;

import com.ylh.core.Constant;
import com.ylh.core.InitData;
import com.ylh.user.entity.UserDO;
import org.apache.catalina.User;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * @ClassName UserDao
 * @Description
 * @Author yanglh
 * @Date 2020/2/23 21:44
 */
@Repository
public class UserDao {
    @Resource
    private InitData initData;

    public UserDO getUser(String name,String password){
        UserDO user = initData.getUsers()
                              .stream()
                              .filter(userDO -> StringUtils.equals(userDO.getName(),name) &&  StringUtils.equals(userDO.getPassword(),password))
                              .findFirst()
                              .orElse(null);
        return user;
    }

    public int saveUser(UserDO userDO) {
        String now = LocalDateTime.now().format(Constant.FM1);
        userDO.setCreateTime(now);
        initData.getUsers().add(userDO);
        return 1;
    }
}
